package models;

import java.text.ParseException;

import org.cheffo.jeplite.function.PostfixMathCommand;
import org.cheffo.jeplite.util.DoubleStack;

/**
 * This class is used to extend the functionality of the JEP parser
 * by adding a function for retrieving any point from list of points
 * 
 * @author NiTh4r0
 */
public class PointFromTable extends PostfixMathCommand
{
	private AttractorGen			ag			= null;
	private String					coord		= null;
	
	public PointFromTable(AttractorGen ag, String coord)
	{
		numberOfParameters = 2;
		
		this.ag = ag;
		this.coord = coord;
	}
	
	public final void run(DoubleStack inStack)
	{
		// check the stack
		// checkStack(inStack);
		
		// get the parameters from stack
		Object firstParam = inStack.pop();
		Object secondParam = inStack.pop();
		
		// check whether the argument is of the right type
		if (firstParam instanceof Double && secondParam instanceof Double)
		{
			Double offset = (Double) firstParam;
			Double currentPos = (Double) secondParam;
			
			if (coord.equals("X"))
			{
				inStack.push(ag.getX(currentPos.intValue(), offset.intValue()));
			}
			else if (coord.equals("Y"))
			{
				inStack.push(ag.getY(currentPos.intValue(), offset.intValue()));
			}
			else if (coord.equals("Z"))
			{
				inStack.push(ag.getZ(currentPos.intValue(), offset.intValue()));
			}
			else
			{
				try
				{
					throw new ParseException("Invalid first parameter (X, Y, Z are allowed)", 0);
				}
				catch (ParseException e)
				{
					System.err.println(e.getLocalizedMessage());
				}
			}
				
		}
		else
		{
			try
			{
				throw new ParseException("Invalid parameter type", 0);
			}
			catch (ParseException e)
			{
				System.err.println(e.getLocalizedMessage());
			}
		}
	}
}
